import javax.swing.JOptionPane;

/**
 * 
 *is the control class of Add new book Gui.
 */
public class AddNewBookControl 
{
	/** array of strings */
	public String[] addBook; 
	/** array of strings */
	public String [] check;
	
	/**
	 * this method first check if the serial entered by the user does not exist in data-base
	 * then collect the input from the gui and send it forward.
	 */
	public void add_book()
	{
		String temp;
		check = new String[3];
		check[0]=Main.gusers.id;
		check[1]="check";
		check[2]=Main.addNBookG.getJSerialField().getText();;
		Main.log.connection.accept("check_in_dataB");
		while(Main.log.flag==false);
		Main.log.flag=false;
		temp=ChatClient.getRs();
		if(temp.equals("exist"))
		{
			JOptionPane.showMessageDialog(null,"The serial is already exists in database, please change serial number","ERROR",JOptionPane.ERROR_MESSAGE);
			return;
		}
		if(temp.equals("error"))
		{
			JOptionPane.showMessageDialog(null,"There is a problem with the serial number you have entered\n please check the number and try again ","ERROR",JOptionPane.ERROR_MESSAGE);
			return;
		}
		else
		{
			addBook = new String[15];
			addBook[0]=Main.myIP;
			addBook[1]="add_new_book";
			addBook[2]=Main.addNBookG.getJSerialField().getText();
			addBook[3]=Main.addNBookG.getJTitleField().getText();
			addBook[4]=Main.addNBookG.Summery.getText();	
			addBook[5]=Main.addNBookG.getJKeyWordsField().getText();
			addBook[6]=Main.addNBookG.getJIndexField().getText();
			if(Main.addNBookG.jCheckPdf.isSelected())
				addBook[7]="yes";
			else
				addBook[7]="no";
			if(Main.addNBookG.jCheckDoc.isSelected())
				addBook[8]="yes";
			else
				addBook[8]="no";
			if(Main.addNBookG.jCheckFb2.isSelected())
				addBook[9]="yes";
			else
				addBook[9]="no";
			addBook[10]=Main.addNBookG.getJPrice1Field().getText();
			addBook[11]=Main.addNBookG.getJLanguageField().getText();
			addBook[12]=Main.addNBookG.getJAuthorField().getText();	
			if (Main.addNBookG.SubjectCombo2.getSelectedItem().equals(" "))
			{
				JOptionPane.showMessageDialog(null,"you must choose a subject for the book","ERROR",JOptionPane.ERROR_MESSAGE);
				return;
			}
			else
				addBook[13]=(String) Main.addNBookG.SubjectCombo2.getSelectedItem();
			if (Main.addNBookG.FieldCombo2 == null )
			{
				JOptionPane.showMessageDialog(null,"you must choose a field for the book","ERROR",JOptionPane.ERROR_MESSAGE);
				return;
			}
			else
				addBook[14]=(String) Main.addNBookG.FieldCombo2.getSelectedItem();
			if(addBook[2].equals("") || addBook[3].equals("") || addBook[4].equals("") || addBook[5].equals("") || 
					addBook[6].equals("") || addBook[10].equals("") || addBook[11].equals("") || addBook[12].equals("") ||
					(addBook[7].equals("no") && addBook[8].equals("no") && addBook[9].equals("no")) || addBook[13].equals(" ") || addBook[14].equals(" "))
			{
				 JOptionPane.showMessageDialog(null,"One or more fields are empty !","ERROR",JOptionPane.ERROR_MESSAGE);
				 return;
			}
			
			Main.log.connection.accept("addbook");
			while(Main.log.flag==false);
			Main.log.flag=false;
			if(temp.equals("error"))
				JOptionPane.showMessageDialog(null,"The book was not added, please try again","ERROR",JOptionPane.ERROR_MESSAGE);
			else
			{
				JOptionPane.showMessageDialog(null,"The Book was added to Database","INFO",JOptionPane.INFORMATION_MESSAGE);
				if(JOptionPane.showConfirmDialog(null, "Do you wish to attach more Subjects to this book ?","Confirmation", 0) == 0)
				{	//addBook[2]					
					Main.addNBookG.setEnabled(false);
					Main.addNBookG.setVisible(false);
					Main.searchBookG = new SearchBookGUI();
					Main.searchBookC = new SearchBookCONTROL();
					Main.log.frame.setSize(530,340);
					Main.log.frame.add(Main.searchBookG);
					Main.UpdBooksSubjectCTRL =new UpdateBookSubjectCTRL();
					Main.UpdBooksSubjectGui =new UpdateBookSubject(addBook[2]);
					//Main.UpdBooksSubjectGui =new UpdateBookSubject("1000");
					Main.log.frame.add(Main.UpdBooksSubjectGui.panel);			
					Main.UpdBooksSubjectGui.panel.setEnabled(true);
					Main.UpdBooksSubjectGui.panel.setVisible(true);
					return;
				}
			Main.addNBookG.setEnabled(false);
			Main.addNBookG.setVisible(false);
			Main.searchBookG = new SearchBookGUI();
			Main.searchBookC = new SearchBookCONTROL();
			Main.log.frame.setSize(720,560);
			Main.log.frame.add(Main.searchBookG);
			Main.searchBookG.setEnabled(true);
			Main.searchBookG.setVisible(true);
			return;
			}	
		}
	}
}
